﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace AutoFillCenter
{
    class DllLoader
    {
        #region Constant

        public const int WM_COMMAND = 0x111;
        public const int WM_SETTEXT = 0x000C;
        public const int BM_CLICK = 0x00F5;

        #endregion

        #region Function From User32.dll

        [DllImport("user32.dll")]
        public static extern IntPtr GetFocus();

        [DllImport("User32.dll")]
        public static extern int FindWindow(string strClassName, string strWindowName);

        [DllImport("User32.dll")]
        public static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string strClassName, string strWindowName);

        [DllImport("User32.dll")]
        public static extern Int32 SendMessage(
            int hWnd,
            int Msg,
            int wParam,
            [MarshalAs(UnmanagedType.LPStr)]string lParam);


        [DllImport("User32.dll")]
        public static extern Int32 SendMessage(
            int hWnd,
            int Msg,
            int wParam,
            int lParam);

        [DllImport("User32.dll")]
        public static extern int GetWindowText(
            int hWnd,
            StringBuilder lpString,
            int nMaxCount);

        [DllImport("User32.dll")]
        public static extern int GetWindowTextLength(
            int hWnd);

        [DllImport("User32.dll")]
        public static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount);

        public delegate bool Win32Callback(int hwnd, int lParam);
        [DllImport("user32.Dll")]
        public static extern bool EnumChildWindows(int parentHandle, Win32Callback callback, int lParam);

        delegate bool CallbackDef(int hWnd, int lParam);
        [DllImport("user32.dll")]
        static extern int EnumWindows(
            CallbackDef callback,
            int lParam);

        [DllImport("user32.dll")]
        public static extern int GetParent(
            int hWnd);

        [DllImport("User32.dll")]
        public static extern int GetForegroundWindow();

        [DllImport("User32.dll")]
        public static extern int SetFocus(int hWnd);


        [DllImport("User32.dll")]
        public static extern bool IsWindowEnabled(int hWnd);

        [DllImport("User32.dll")]
        public static extern bool SetForegroundWindow(int hWnd);

        #endregion

        public static void PrintParentPath(int hWnd){
            int id = 0;

            int parent = hWnd;

            while (parent != id)
            {
                parent = DllLoader.GetParent(parent);
                StringBuilder temp = new StringBuilder(128);
                DllLoader.GetClassName(parent, temp, temp.MaxCapacity);
                Console.WriteLine(parent.ToString("X") + "__" + temp.ToString());
            }
        }
    }
}
